package com.huike.xingxing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huike.xingxing.R;
import com.huike.xingxing.api.Api;
import com.huike.xingxing.bean.Contact;
import com.huike.xingxing.bean.Warning;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

public class AdvisoryActivity extends BaseActivity {

    private ImageView iv_title_black;
    private TextView tv_title_txt_black;
    private TextView tv_title_txt_content;
    private ImageView iv_title_right_image;
    private TextView tv_title_right_txt;
    private ListView lv_advisory;
    private RequestQueue queue;
    private Contact contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advisory);
        queue = Volley.newRequestQueue(this);
        initView();
        initData();


        //点击返回
        iv_title_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //listView 点击 跳转对应页面
        lv_advisory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Contact.DataBean bean = contact.getData().get(position);
               EventBus.getDefault().postSticky(bean);
               startActivity(new Intent(AdvisoryActivity.this,AdvisoryDescActivity.class));
            }
        });
    }

    private void initData() {
            showLoading();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.locaApi + "getContact", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                contact = new Gson().fromJson(jsonObject.toString(), Contact.class);
                Log.e("---",contact.getData().toString());
                lv_advisory.setAdapter(new ListViewAdapter());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(AdvisoryActivity.this, "网络异常！", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }

    private void initView() {
        iv_title_black = (ImageView) findViewById(R.id.iv_title_black);
        tv_title_txt_black = (TextView) findViewById(R.id.tv_title_txt_black);
        tv_title_txt_content = (TextView) findViewById(R.id.tv_title_txt_content);
        iv_title_right_image = (ImageView) findViewById(R.id.iv_title_right_image);
        tv_title_right_txt = (TextView) findViewById(R.id.tv_title_right_txt);
        lv_advisory = findViewById(R.id.lv_advisory);

        tv_title_txt_content.setText("咨询");
        iv_title_black.setVisibility(View.VISIBLE);


    }

    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return contact.getData().size();
        }

        @Override
        public Object getItem(int position) {
            return contact.getData().get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(AdvisoryActivity.this, R.layout.lv_advisory, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Contact.DataBean bean = contact.getData().get(position);


            //图片加载
            Glide.with(convertView).load(Api.url + bean.getImage()).into(holder.iv_advisry_lv_image);

            holder.iv_advisry_lv_title.setText(bean.getTitle());
            holder.iv_advisry_lv_desc.setText(bean.getMessages());

            return convertView;
        }

        class ViewHolder {
            public View rootView;
            public ImageView iv_advisry_lv_image;
            public TextView iv_advisry_lv_title;
            public TextView iv_advisry_lv_desc;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.iv_advisry_lv_image = (ImageView) rootView.findViewById(R.id.iv_advisry_lv_image);
                this.iv_advisry_lv_title = (TextView) rootView.findViewById(R.id.iv_advisry_lv_title);
                this.iv_advisry_lv_desc = (TextView) rootView.findViewById(R.id.iv_advisry_lv_desc);
            }

        }
    }
}
